package com.thexb.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thexb.common.Result;
import com.thexb.sys.entity.User;
import com.thexb.sys.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author thexb
 * @since 2023-04-04
 */
@Api(tags = {"用户接口列表"})
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/all")
    public Result<List<User>> getAllUser() {
        List<User> list = userService.list();
        return Result.success(list, "查询成功");
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody User user) {
        System.out.println(user);
        HashMap<String, Object> map = userService.login(user);
        if (map == null) {
            return Result.fail(map, "用户名或者密码不对");
        }
        return Result.success(map, "登陆成功");
    }

    @GetMapping("/info")
    public Result<Map<String, Object>> getInfo(@RequestParam("token") String token) {
        HashMap<String, Object> info = userService.getInfo(token);
        if (info == null) {
            return Result.fail(info, "用户名不存在");
        }
        return Result.success(info, "获取用户信息成功");
    }

    @PostMapping("/logout")
    public Result<Map<String, Object>> logout(@RequestHeader("X-Token") String token) {
        userService.logout(token);
        return Result.success(null, "用户退出成功");
    }

    @GetMapping("/list")
    public Result<Map<String, Object>> getUserList(@RequestParam(value = "username", required = false) String username,
                                                   @RequestParam(value = "phone", required = false) String phone,
                                                   @RequestParam(value = "pageNo") Long pageNo,
                                                   @RequestParam(value = "pageSize") Long pageSize) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(username), User::getUsername, username);
        queryWrapper.eq(StringUtils.hasLength(phone), User::getPhone, phone);
        queryWrapper.orderByDesc(User::getId);

        Page<User> page = new Page<>(pageNo, pageSize);
        userService.page(page, queryWrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("rows", page.getRecords());
        return Result.success(map);
    }

    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping
    public Result<Map<String, Object>> addUser(@RequestBody User user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userService.addUser(user);
        return Result.success(null, "新增用户成功");
    }
    @PutMapping
    public Result<Map<String, Object>> updateUser(@RequestBody User user) {
        System.out.println(user);
        userService.updateUser(user);
        return Result.success(null, "修改用户成功");
    }
    @GetMapping("/{id}")
    public Result<Map<String, Object>> getUserById(@PathVariable("id") int id) {
        User user = userService.getUser(id);
        user.setPassword(null);
        HashMap<String, Object> map = new HashMap<>();
        map.put("user", user);
        return Result.success(map, "获取用户成功");
    }
    @DeleteMapping("/{id}")
    public Result<Map<String, Object>> removeUserById(@PathVariable("id") int id) {
        userService.removeUser(id);
        return Result.success(null, "删除用户成功");
    }
}
